// MARK: - Xcode
// 单句执行：shift + enter
// 全部执行：shift + cmd + enter

// MARK: - 需求
// 清空console

import Foundation

// MARK: - Bool
var bool = true
bool.toggle()
//print(bool)

// MARK: - Range
let close = 0...5
//print(close)
let halfOpen = 0..<5
//print(halfOpen)

// MARK: - Switch
let i = 123
var string = ""
switch i {
case let x where x % 10 == 0:
    string = "\(x) % 10 == 0"
case _ where i % 2 == 0:
    string = "\(i) even"
case 50..<100:
    string = "\(i) 50..<100"
default:
    string = String(i)
}
//print(string)

let point3d = (x: 1, y: 2, z: 3)
var point3dString = ""
switch point3d {
case (let x, 0, 0):
    point3dString = "x:\(x), 0, 0"
case (0, _, 0):
    point3dString = "0, _, 0"
case (let x, let y, 0) where x == y:
    point3dString = "x == y, z == 0"
default:
    point3dString = "\(point3d.x), \(point3d.y), \(point3d.z)"
}
//print(point3dString)

// MARK: - Loop
for i in 0..<10 where i % 2 == 0 {
//    print(i)
}

// MARK: - Function
func add1(_ value: inout Int) {
    value += 1
}
var value = 123
add1(&value)
//print(value)

var closure = {(a: Int, b: Int) -> Int in
    return a * b
}
closure = {(a: Int, b: Int) -> Int in
    a * b
}
closure = {(a, b) in
    a * b
}
closure = {
    $0 * $1
}
//print(closure(2, 3))
/*
closure = {[weak self] (a, b) in
    self.xxx...
}
*/

func operate(_ a: Int, _ b: Int, operation: (Int, Int) -> Int) -> Int {
    return operation(a, b)
}
let operateResult1 = operate(4, 5, operation: { $0 + $1 })
//print(operateResult1)
let operateResult2 = operate(6, 7) { $0 + $1 }
//print(operateResult2)

func countingClosure() -> () -> Int {
    var counter = 0
    let incrementCounter: () -> Int = {
        counter += 1
        return counter
    }
    return incrementCounter
}
let counter1 = countingClosure()
let counter2 = countingClosure()
counter1() // 1
counter2() // 1
counter1() // 2
counter1() // 3
counter2() // 2

// MARK: - 异步闭包
// 异步闭包用@escaping标记，并且要注意self的循环引用
func execute(backgroundWork: @escaping () -> Int, mainWork: @escaping (Int) -> ()) {
    DispatchQueue.main.async {
        let result = backgroundWork()
        DispatchQueue.main.async {
            mainWork(result)
        }
    }
}

// MARK: - Random
let r = Int.random(in: 0..<1000)
//print(r)

// MARK: - 其他
let bigNumber = 1_000_000
//print(bigNumber)

let bigString = """
    123
    456
    789
    """
//print(bigString)

